package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.management.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/depts")
@SuppressWarnings("all")
@Slf4j
public class DeptController {
    @Autowired
    private DeptService deptService;

    @DeleteMapping("/{id}")
    public Result deleteDept(@PathVariable Integer id) {
        deptService.deleteById(id);
        log.info("删除部门id:{}", id);
        return Result.success();
    }

    @PutMapping
    public Result updateDept(@RequestBody Dept dept) {
        deptService.update(dept);
        log.info("修改部门Dept:{}",dept );
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getDept(@PathVariable Integer id) {
        Dept dept = deptService.getById(id);
        log.info("查询部门id:{}", id);
        return Result.success(dept);
    }

    @GetMapping
    public Result page(
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "status", required = false) Integer status,
            @RequestParam(name = "page",  defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize",  defaultValue = "10") Integer pageSize) {
        PageResult<Dept> pageResult = deptService.page(name, status, page, pageSize);
        log.info("分页查询部门，参数：name={},status={},page={},pageSize={}", name, status, page, pageSize);
        return Result.success(pageResult);
    }




    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        deptService.addDept(dept);
        log.info("添加部门Dept:{}",dept );
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        return Result.success(deptService.list());
    }
}